package com.itcast.catchfiveeight;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ade on 2016/2/21.
 */
public class CatchLinks {


    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String urlFather = "http://MMMMMM.58.com/job/?key=XXXXXX&cmcskey=XXXXXX&final=1&jump=2&specialtype=gls&canclequery=isbiz%3D0&sourcetype=4";
        //读取Data58.txt文件,获取到所有的58城市的数据
        File file = new File(Contents.txtPath);
        List<String> list = FileUtils.readLines(file,"GBK");
        List<Map<String,String>> cityList =  new ArrayList<Map<String,String>>();
        for(String str : list){
            Map<String,String> map = new HashMap<String,String>();
            String[] m =str.split(",");
            map.put("ecity",m[0]);
            map.put("ccity",m[1]);
            cityList.add(map);
        }
         List<String> keyWordList =  Contents.getkeyWordList();
        //这里就是循环操作每个关键字代码还有城市代码
        for(Map<String,String> city : cityList){
            for(String str : keyWordList){
                File files =  createFile(city.get("ccity")+".csv");
                BolgBody(urlFather,city,str,files);
            }
        }
    }

    /**
     * 获取指定HTML 文档指定的body
     * @throws IOException
     */
    private static void BolgBody(String url,Map<String,String> cityMap,String keyword,File file) throws Exception {

        String keywordUtf8 = URLEncoder.encode(keyword,"utf-8");
        // 从 URL 直接加载 HTML 文档
        url = url.replace("MMMMMM",cityMap.get("ecity"));
        url = url.replace("XXXXXX",keywordUtf8);
        //得到这个网站的主页
        int indexInt = url.indexOf("58.com");
        String indexUrl =  url.substring(0,indexInt+6);
        Map<String,String> cityKeyMap = new HashMap<String,String>();
        cityKeyMap.put("ccity",cityMap.get("ccity"));
        cityKeyMap.put("keyword",keyword);
        seeNextUrl(indexUrl,url,file,cityKeyMap);
    }


    /**
     *访问下一页的递归方法
     */
    public static void seeNextUrl(String indexUrl,String nextUrl,File file,Map<String,String> map){
        System.out.println(nextUrl);
        try {
            Document doc = Jsoup.connect(nextUrl).get();//总页
            Elements body = doc.select("#infolist"); //查看选择列表
            Elements elementsLinks =  body.select("a[href$=.shtml]");
            String catTBlink = "";
            for(Element link : elementsLinks){
                catTBlink = link.attr("href");
                //这里是应该在文件里面写的东西，调取文件写入方法
                writeFile(file,catTBlink,map);
            }
            //获取下一页链接
            String nextUrls = getNextUrl(indexUrl,nextUrl);
            if(nextUrls.length() > 0){//如果getNextUrl能获取到
                seeNextUrl(indexUrl,nextUrls,file,map);
            }else{
                return;
            }
            //继续获取下一页的链接
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *根据当前的页的URL获取下一页的URL
     */

    public static String getNextUrl(String indexUrl,String recentUrl){
        Document doc = null;//总页
        try {
            doc = Jsoup.connect(recentUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String nextUrl = "";
        if(doc.select(".pager .next").size() > 0){
            Element elementNext = doc.select(".pager .next").get(0);
            nextUrl = indexUrl +elementNext.attr("href");
        }
        return nextUrl;
    }

    /**
     * 创建文件
     */
    public static File createFile(String fileName){
        String filePath = Contents.linkPath;
        File file = new File(filePath,fileName);
        return file;
    }

    /**
     *
     * @param file
     * @param link
     */
    public static void writeFile(File file ,String link,Map<String,String> map){

        try {
            //写入链接方法
              writeLink(file,link,map);
            //直接写入属性方法，应该是内存有问题，所以还是先保存链接再做此操作
            //writeTitle(file);
            //writeProperty(file,link,map);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //写入一个元素适配器
    public static void writeElement(File file,String element){
        try {
            FileUtils.write(file, "\"", "GBK", true);
            FileUtils.write(file, element, "GBK", true);
            FileUtils.write(file, "\"," , "GBK", true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 只写入链接方法,包含城市还有关键字
     * @param file
     * @param link
     * @param map
     * @throws IOException
     */
    public static void writeLink(File file,String link,Map<String,String> map) throws IOException {
        //
        FileUtils.write(file, map.get("ccity"), "GBK", true);
        FileUtils.write(file, ",", "GBK", true);
        FileUtils.write(file, link, "GBK", true);
        FileUtils.write(file, ",", "GBK", true);
        FileUtils.write(file, map.get("keyword"), "GBK", true);
        FileUtils.write(file, "\r\n", "GBK", true);
    }

    /**
     * 写入属性方法
     * @param file
     * @param link
     * @param keyword
     * @throws IOException
     */
    public static void writeProperty(File file,String link,String keyword,String city) throws IOException {
        GetPage getIndex = new GetPage();
        Page entity = getIndex.get58Data(link);

        writeElement(file,link);
        writeElement(file,entity.geteStrCompany());
        writeElement(file,entity.geteStrHangye());
        writeElement(file,entity.geteStrGuimo());
        writeElement(file,entity.geteStrXingzhi());
        writeElement(file,entity.geteStrAddress());
        writeElement(file,entity.geteStrPhone());
        writeElement(file,entity.geteStrMan());
        writeElement(file,city);
        writeElement(file,keyword);
        FileUtils.write(file, "\r\n", "GBK", true);
    }

    /**
     * 写入属性的标题
     * @param file
     * @throws IOException
     */
    public static void writeTitle(File file) throws IOException {
        //"链接","公司名称","行业","规模","性质","工作地址","联系电话","联系人","城市","关键字"
        writeElement(file,"链接");
        writeElement(file,"公司名称");
        writeElement(file,"行业");
        writeElement(file,"规模");
        writeElement(file,"性质");
        writeElement(file,"工作地址");
        writeElement(file,"联系电话");
        writeElement(file,"联系人");
        writeElement(file,"城市");
        writeElement(file,"关键字");
        FileUtils.write(file, "\r\n", "GBK", true);
    }



}
